//
//  ZYMusicTool.m
//  ZYMusicPlayer
//
//  Created by 王志盼 on 15/10/12.
//  Copyright © 2015年 王志盼. All rights reserved.
//

#import "ZYMusicTool.h"
#import "ZYMusic.h"
#import <MediaPlayer/MediaPlayer.h>
static NSArray *_musics;
static NSDictionary *_aritstMusics;
static ZYMusic *_playingMusic;
static BOOL _random;
static BOOL _repeat;
@implementation ZYMusicTool

+ (NSArray *)musics
{
    if (_musics == nil) {
        _musics = [self getItunesMusic];
    }
    return _musics;
}

+ (NSDictionary *)aritstMusics
{
    if (_aritstMusics == nil) {
        _aritstMusics = [self getArtistMusic];
    }
    return _aritstMusics;
}

+ (NSMutableArray *)getItunesMusic {
    NSMutableArray *musicList = [[NSMutableArray alloc] init];
    // 创建媒体选择队列
    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    // 创建读取条件
    MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic] forProperty:MPMediaItemPropertyMediaType];
    // 给队列添加读取条件
    [query addFilterPredicate:albumNamePredicate];
    // 从队列中获取条件的数组集合
    NSArray *itemsFromGenericQuery = [query items];
    // 遍历解析数据
    for (MPMediaItem *music in itemsFromGenericQuery) {
        ZYMusic *zymusic = [self resolverMediaItem:music];
        [musicList addObject:zymusic];
    }
    return musicList;
}

+ (NSMutableDictionary *)getArtistMusic {
    NSMutableDictionary *musicDic = [[NSMutableDictionary alloc] init];
    // 创建媒体选择队列
    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    // 创建读取条件
    MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic] forProperty:MPMediaItemPropertyMediaType];
    // 给队列添加读取条件
    [query addFilterPredicate:albumNamePredicate];
    // 从队列中获取条件的数组集合
    NSArray *itemsFromGenericQuery = [query items];
    // 遍历解析数据
    for (MPMediaItem *music in itemsFromGenericQuery) {
        ZYMusic *zymusic = [self resolverMediaItem:music];
        NSMutableArray *arry = [musicDic objectForKey:zymusic.album];
        if (arry && arry.count > 0) {
            [arry addObject:zymusic];
        } else {
            arry = [[NSMutableArray alloc] init];
            [arry addObject:zymusic];
        }
        [musicDic setObject:arry forKey:zymusic.album];
    }
    return musicDic;
}

+ (ZYMusic *)resolverMediaItem:(MPMediaItem *)music {
    // 歌曲路径
    NSURL *fileURL = [music valueForProperty:MPMediaItemPropertyAssetURL];
    NSString *fileString = [[NSString alloc] initWithFormat:@"%@", fileURL ? fileURL : @""];
    // 歌手名字
    NSString *singer = [music valueForProperty:MPMediaItemPropertyArtist];
    // 歌名
    NSString *name = [music valueForProperty:MPMediaItemPropertyTitle];
    // 歌曲时长（单位：秒）
    NSString *album = [music valueForProperty:MPMediaItemPropertyAlbumArtist];
    NSTimeInterval duration = [[music valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue];
    NSString *time = @"";
    if((int)duration % 60 < 10) {
        time = [NSString stringWithFormat:@"%d:0%d",(int)duration / 60,(int)duration % 60];
    } else {
        time = [NSString stringWithFormat:@"%d:%d",(int)duration / 60,(int)duration % 60];
    }
    // 歌曲插图（没有就返回 nil）
    MPMediaItemArtwork *artwork = [music valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *image;
    if (artwork != nil) {
        image = [artwork imageWithSize:CGSizeMake(72, 72)];
    } else {
        image = [UIImage imageNamed:@"play_cover_pic_bg.png"];
    }

    NSDictionary *musicDic = @{@"name": name ? name : @"未知歌曲",
                              @"filename": fileString,
                              @"singer": singer ? singer : @"未知歌手",
                               @"time": time ? time : 0,
                               @"icon": image ? image : [UIImage imageNamed:@"play_cover_pic_bg.png"],
                              @"lrcname": @"",
                              @"singerIcon" : image ? image : [UIImage imageNamed:@"play_cover_pic_bg.png"],
                               @"album" : album ? album : @"未知专辑"
                            };
    ZYMusic *zymusic = [ZYMusic yy_modelWithDictionary:musicDic];
    zymusic.item = music;
    return zymusic;
}
@end
